package com.tencreat.mall.controller.admin;

import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.entity.SysUser;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.poi.ExcelNewUtil;
import com.tencreat.domain.account.MallStore;
import com.tencreat.domain.mall.MallOrder;
import com.tencreat.domain.mall.vo.*;
import com.tencreat.service.account.IMallStoreService;
import com.tencreat.service.mall.IMallOrderService;
import com.tencreat.service.system.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单-店铺
 * @author zixin
 */
@RestController
@RequestMapping("/mall/store-order")
@Api(tags = "订单-店铺")
public class MallStoreOrderController extends BaseController {

    @Autowired
    private IMallOrderService mallOrderService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IMallStoreService mallStoreService;

    @ApiOperation(value = "订单列表")
    @PreAuthorize("@ss.hasPermi('mall:store-goods:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallOrder>> list(@RequestBody MallOrder mallOrder) {
        Page<MallOrder> page = new Page<>(mallOrder.getPageNum(), mallOrder.getPageSize());
//        mallOrder.setUserId(getLoginCheck().getUser().getUserId());
        MallStore mallStore = mallStoreService.lambdaQuery().eq(MallStore::getUserId, getLoginCheck().getUser().getUserId()).one();
        if (mallStore!=null){
            mallOrder.setStoreId(mallStore.getId());
        }
        return HttpResult.ok(mallOrderService.page(page,mallOrder));
    }

    @ApiOperation(value = "订单管理数据统计")
    @GetMapping("/storeOrderStatistics")
    public HttpResult<MallAdminOrderStatisticsVO> storeOrderStatistics(@ApiParam("订单标记 4-虚拟订单") @RequestParam(required = false) Integer orderMold){
        Long userId = getLoginCheck().getUser().getUserId();
        return HttpResult.ok( mallOrderService.orderStatistics(userId,orderMold));
    }

    @ApiOperation(value = "订单列表-导出")
    @PreAuthorize("@ss.hasPermi('mall:store-order:export')")
    @PostMapping("/export")
    @Log(title = "订单管理>订单-店铺", businessType = BusinessType.EXPORT)
    public void export(@RequestBody @Validated MallOrder dto, HttpServletResponse response) {
        dto.setUserId(getLoginCheck().getUser().getUserId());
        Page<MallOrder> page = new Page<>(-1, -1);
        page = mallOrderService.page(page, dto);
        if (dto.getType() == 1){
            List<MallStoreOrderVO> list = CglibUtil.copyList(page.getRecords(), MallStoreOrderVO::new);
            ExcelNewUtil.exportExcel(list,"订单发货",MallStoreOrderVO.class, response);
        }
        if (dto.getType() == 0){
            List<MallSupplierOrderVO2> list = CglibUtil.copyList(page.getRecords(), MallSupplierOrderVO2::new);
            ExcelNewUtil.exportExcel(list,"订单列表",MallSupplierOrderVO2.class, response);
        }
        if (dto.getType() == 3){
            List<MallSupplierOrderVO5> list = CglibUtil.copyList(page.getRecords(), MallSupplierOrderVO5::new);
            ExcelNewUtil.exportExcel(list,"虚拟订单",MallSupplierOrderVO5.class, response);
        }
    }

    @ApiOperation(value = "首页统计")
    @PostMapping("/orderStatistics")
    public HttpResult<OrderStatisticsVO> orderStatistics() {
        return HttpResult.ok(mallOrderService.supplierOrderStatistics(getLoginCheck().getUser().getUserId()));
    }


    @ApiOperation(value = "核销")
    @PreAuthorize("@ss.hasPermi('mall:store-order:check')")
    @PostMapping("/check")
    public HttpResult<Boolean> check(@RequestBody MallOrder mallOrder) {
        Long userId = getLoginCheck().getUser().getUserId();
        MallStore mallStore = mallStoreService.lambdaQuery().eq(MallStore::getUserId, userId).one();
        if (mallStore!=null){
            mallOrder.setStoreId(mallStore.getId());
        }
        mallOrder.setUserId(userId);
        return mallOrderService.check(mallOrder);
    }


    @ApiOperation(value = "店铺订单数据统计")
    @PostMapping("/storeOrderStatistics")
    public HttpResult<MallAdminOrderStatisticsVO> storeOrderStatistics(@RequestBody ReqIdParam idParam){
        Long userId = getLoginCheck().getUser().getUserId();

        MallStore mallStore = mallStoreService.lambdaQuery().eq(MallStore::getUserId, userId).one();

        return HttpResult.ok( mallOrderService.storeOrderStatistics(userId,idParam));
    }

    @ApiOperation(value = "店铺货款数据统计")
    @PostMapping("/storeGoodsPayment")
    public HttpResult<MallAdminOrderStatisticsVO> supplierOrderStatistics(@RequestBody ReqIdParam idParam){
        Long userId = getLoginCheck().getUser().getUserId();

        return HttpResult.ok( mallOrderService.storeOrder(userId,idParam));
    }

}
